
package com.ifcar.app.activity.mycomment;

import com.ifcar.app.activity.MyCommentsActivity;
import com.ifcar.app.fragment.NRFragment;
import com.ifcar.app.fragment.NRLoadingFrament;
import com.ifcar.app.fragment.NRTabFragment;
import com.ifcar.app.fragment.NRTabFragment.TabManager.TabInfo;
import com.ifcar.app.util.ThemeSettingsHelper;
import com.ifcar.app.view.LoadingListView;
import com.ifcar.app.view.MyTabWidget;
import com.ifcar.app.view.MyTabWidget.AnimationViewCallback;
import com.ifcar.app.activity.R;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

public class MyCommentTabFragment extends NRTabFragment {
    public static final String TAG = "myCommentTab";
    
    public static final String TAB_MY_COMMENT = "mycomment";

    public static final String TAB_REPLY_ME = "replyme";
    
//    private LoadingListView mLoadingListView;
    
    private String mLastTab;
    
    private String mAccount;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setNRMenu(R.layout.mycomment_menu, new int[] {
                R.id.menu_refresh, R.id.menu_logout
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.mycomment_tab_layout, container, false);
    }

    @Override
    public int getContainerId() {
        return R.id.realtabcontent;
    }
    
//    @Override
//    public void onViewCreatedNR(View view, Bundle savedInstanceState) {
//        super.onViewCreatedNR(view, savedInstanceState);
//        mLoadingListView = (LoadingListView) view
//                .findViewById(R.id.my_comment_loading_list);
//
//        LayoutInflater inflate = LayoutInflater.from(getActivity());
//        View moreView = inflate.inflate(R.layout.more_layout,
//                mLoadingListView.getListView(), false);
//        mLoadingListView.setLoadingMoreView(moreView, R.id.show_more_button);
//    }
//
//    @Override
//    public void onTabChanged(String tabId) {
//        super.onTabChanged(tabId);
//
//        if (!TextUtils.isEmpty(mLastTab)) {
//            MyCommentFragment f = (MyCommentFragment) getTabManager().getTabInfos()
//                    .get(mLastTab).getFragment();
//            f.detachLoadingListView();
//        }
//
//        mLastTab = tabId;
//
//        MyCommentFragment f = (MyCommentFragment) getTabManager().getTabInfos()
//                .get(tabId).getFragment();
//
//        f.attachLoadingListView(mLoadingListView);
//    }
    
    @Override
    protected void initTabhost(TabManager tabManager, TabHost tabHost) {
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        MyTabWidget tabWidget = (MyTabWidget) tabHost.getTabWidget();
        View animationView = inflater.inflate(
                R.layout.comment_tab_indicator_bg, null);

        tabWidget.setAnimaitonView(animationView, new AnimationViewCallback() {

            @Override
            public void onPrepareMeasure(View currentTabView, View animationView) {
                // View indicatorView =
                // currentTabView.findViewById(R.id.comment_tab_indicator);
                View indicatorBgView = animationView
                        .findViewById(R.id.indicator_bg);
                ViewGroup.LayoutParams lp = indicatorBgView.getLayoutParams();
                lp.width = indicatorBgView.getBackground().getIntrinsicWidth();// indicatorView.getMeasuredWidth();
                lp.height = indicatorBgView.getBackground()
                        .getIntrinsicHeight();// indicatorView.getMeasuredHeight();
            }

            @Override
            public Animation createAnimation(int current, int last) {
                TranslateAnimation animation = new TranslateAnimation(
                        Animation.RELATIVE_TO_SELF, last - current,
                        Animation.RELATIVE_TO_SELF, 0,
                        Animation.RELATIVE_TO_SELF, 0,
                        Animation.RELATIVE_TO_SELF, 0);
                animation.setDuration(60);
                animation.setInterpolator(getActivity(),
                        android.R.anim.decelerate_interpolator);
                return animation;
            }

        });

        Bundle args = getArguments();
        mAccount = args.getString("account");
        
        args = new Bundle();
        args.putString("account", mAccount);
        addSpec(tabManager, tabHost, TAB_MY_COMMENT, R.string.comment_myself,
                MyCommentFragment.class, inflater, args);

        args = new Bundle();
        args.putString("account", mAccount);
        addSpec(tabManager, tabHost, TAB_REPLY_ME,
                R.string.comment_replyme, MyCommentFragment.class,
                inflater, args);
    }
    
    private void addSpec(TabManager tabManager, TabHost tabHost, String tag,
            int title, Class clazz, LayoutInflater inflater, Bundle bundle) {
        TabSpec spec = tabHost.newTabSpec(tag);
        View view = inflater.inflate(R.layout.comment_tab_indicator, null);

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.weight = 1;
        view.setLayoutParams(lp);

        TextView tabIndicator = (TextView) view
                .findViewById(R.id.comment_tab_indicator);
        tabIndicator.setText(title);
        spec.setIndicator(view);

        TabInfo info = new TabInfo(tag, clazz, bundle);

        tabManager.addTab(spec, info);
    }

    

    @Override
    public void onMenuClick(int id) {
        super.onMenuClick(id);
        switch (id) {
            case R.id.menu_refresh:
                FragmentManager fm = getFragmentManager();
                NRFragment fragment = (NRFragment) fm.findFragmentByTag(getTabHost()
                        .getCurrentTabTag());
                if (fragment == null) {
                    return;
                }
                ((NRLoadingFrament) fragment).refresh();
                break;
            case R.id.menu_logout:
                ((MyCommentsActivity)getActivity()).logout();
            default:
                break;
        }
    }

    @Override
    public void onApplyTheme() {
        super.onApplyTheme();

        ThemeSettingsHelper themeSettingsHelper = getThemeSettingsHelper();
        Activity context = getActivity();

        TabHost tabHost = getTabHost();
        MyTabWidget tabWidget = (MyTabWidget) tabHost.getTabWidget();

        themeSettingsHelper.setViewBackgroud(context, tabWidget,
                R.drawable.top_column_bg);

        View indicatorBgView = tabWidget.getAnimationView().findViewById(
                R.id.indicator_bg);
        themeSettingsHelper.setViewBackgroud(context, indicatorBgView,
                R.drawable.comment_column_highlight);
        final int count = tabWidget.getTabCount();
        for (int i = 0; i < count; i++) {
            TextView t = (TextView) tabWidget.getChildTabViewAt(i)
                    .findViewById(R.id.comment_tab_indicator);
            themeSettingsHelper.setTextViewColor(context, t,
                    R.color.column_item_color);
        }

//        themeSettingsHelper.setListViewDivider(context,
//                mLoadingListView.getListView(), R.drawable.list_divider_line);
//
//        Button moreButton = (Button) mLoadingListView.getLoadingMoreClickView();
//        themeSettingsHelper.setViewBackgroud(context, moreButton,
//                R.drawable.netease_big_button);
//        themeSettingsHelper.setTextViewColor(context, moreButton,
//                R.color.list_title_color);
        View menuView = getMenuView();
        themeSettingsHelper.setViewBackgroudColor(context,
                menuView.findViewById(R.id.menu_tabrow_1), R.color.menu_layout_bg_color);
        LinearLayout menuItem = (LinearLayout) menuView.findViewById(R.id.menu_refresh);
        applyThemeForMenu(context, themeSettingsHelper, menuItem, true);
        menuItem = (LinearLayout) menuView.findViewById(R.id.menu_logout);
        applyThemeForMenu(context, themeSettingsHelper, menuItem, true);
        
    }
    
    private void applyThemeForMenu(Context context, ThemeSettingsHelper themeSettingsHelper,
            LinearLayout menuView, boolean isUp) {
        themeSettingsHelper.setViewBackgroud(context, menuView, isUp ? R.drawable.menu_background
                : R.drawable.menu_background_below);
        themeSettingsHelper.setTextViewColor(context, (TextView) menuView.getChildAt(1),
                R.color.menu_text_color);

    }
}
